<template>
  <div>
    <van-address-edit
      :address-info="info"
      :area-list="areaList"
      :show-delete="!!$route.query.id"
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择3']"
      @save="onSave"
      @delete="onDelete"
    >
      <div class="van-cell van-field">
        <div class="van-cell__title van-field__label"><span>标签</span></div>
        <div class="van-cell__value van-field__value">
          <div class="van-field__body">
            <van-radio-group v-model="label" direction="horizontal">
              <van-radio name="家">家</van-radio>
              <van-radio name="公司">公司</van-radio>
              <van-radio name="学校">学校</van-radio>
            </van-radio-group>
          </div>
        </div>
      </div>
    </van-address-edit>
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
import { reqAddressAdd, reqAddressDel, reqAddressEdit, reqAddressList } from "../../http/api";
import { Toast } from "vant";
export default {
  data() {
    return {
      areaList,
      label: "家",
      info: {},
    };
  },
  mounted() {
    if (this.$route.query.id) {
      reqAddressList({
        id: this.$route.query.id,
        uid: JSON.parse(localStorage.getItem("isLogin")).uid,
      }).then((res) => {
        if (res.data.code == 200) {
          let info = res.data.list[0];
          this.info = {
            id: info.id,
            name: info.username,
            tel: info.userphone,
            addressDetail: info.useraddress,
            areaCode: info.regioncode,
            isDefault: info.status == 1 ? true : false,
          };
        }
      });
    }
  },
  methods: {
    onSave(content) {
      console.log(content);
      if (this.$route.query.id) {
        //地址编辑
        reqAddressEdit({
          id: content.id,
          username: content.name,
          userphone: content.tel,
          regioncode: content.areaCode,
          location: content.province + content.city + content.county,
          useraddress: content.addressDetail,
          status: content.isDefault ? 1 : 0,
          label: this.label,
        }).then((res) => {
          if (res.data.code == 200) {
            Toast(res.data.msg);
            this.$router.replace("/dizi");
          }
        });
      } else {
        //地址添加
        reqAddressAdd({
          uid: JSON.parse(localStorage.getItem("isLogin")).uid,
          username: content.name,
          userphone: content.tel,
          regioncode: content.areaCode,
          location: content.province + content.city + content.county,
          useraddress: content.addressDetail,
          status: content.isDefault ? 1 : 0,
          label: this.label,
        }).then((res) => {
          if (res.data.code === 200) {
            Toast(res.data.msg);
            this.$router.replace("/dizi");
          }
        });
      }
    },
    onDelete() {
      // 删除逻辑
      reqAddressDel({
        id:this.$route.query.id
      }).then(res=>{
        if(res.data.code==200){
           Toast(res.data.msg);
            this.$router.replace("/dizi");
        }
      })
    },
  },
};
</script>

<style lang="less">
@import "../../less/index.less";
</style>